package com.wondertek.web.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.wondertek.common.base.Result;
import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.dto.app.req.RentalListReq;
import com.wondertek.service.dto.app.res.RentalListRes;
import com.wondertek.service.dto.business.req.*;
import com.wondertek.service.dto.business.res.RentalKvRes;
import com.wondertek.service.dto.business.res.RentalRoomDeviceRes;
import com.wondertek.service.entity.business.DevicePo;
import com.wondertek.service.service.business.DeviceService;
import com.wondertek.service.service.business.RentalService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 租赁设备管理
 * @author: qinjianhui
 * @create: 2024-07-19 11:01
 **/

@RestController
@RequestMapping("/backend/business/rental/")
@Tag(name = "租赁设备管理")
public class RentalDeviceController {

    @Resource
    private DeviceService deviceService;

    @Resource
    private RentalService rentalService;

    /*
     * 查询未租赁的设备 该接口暂时没用到
     * */
    @PostMapping("/select/notRentaldevice")
    @Operation(summary = "查询未租赁的设备")
    public Result<PageInfo<DevicePo>> emailCode(@RequestBody DevicePageReq req){
        return Result.success(deviceService.selectNotRentaldevice(req));
    }

    @PostMapping("/find/rentalList")
    @Operation(summary = "查询租赁管理")
    @PreAuthorize("@el.check('backend:business:rental:find:rentalList')")
    public Result<Page<RentalListRes>> findRentalList(@RequestBody RentalListReq req){
        return Result.success(rentalService.findRentalList(req));
    }

    /**
     * 租赁管理导出
     * @param req
     * @return
     */
    @PostMapping("/findRentalListExport")
    @Operation(summary = "租赁管理导出")
    public void findRentalListExport(@RequestBody RentalListReq req, HttpServletResponse response) {
        rentalService.findRentalListExport(req, response);
    }

    @PostMapping("/find/rentalRoomDevice")
    @Operation(summary = "查询分区租赁、设备租赁")
    @PreAuthorize("@el.check('backend:business:rental:find:rentalRoomDevice')")
    public Result<Page<RentalRoomDeviceRes>> findRentalRoomDevice(@RequestBody RentalRoomDeviceReq req){

        return Result.success(rentalService.findRentalRoomDevice(req));
    }

    /**
     * 添加租赁信息
     * @param req
     */
    @PostMapping("/add/rentalDevice")
    @Operation(summary = "添加租赁")
    @PreAuthorize("@el.check('backend:business:rental:add:rentalDevice')")
    public Result inertRentalDevice(@RequestBody @Validated RentalDeviceAddReq req){
        rentalService.inertRentalDevice(req);
        return Result.success();
    }

    /**
     * 查询所有点位
     * @param id 点位或设备id
     * @param type  1站点 2设备 3分区
     * @param rentalType 1:分区 2:设备
     * @return
     */
    @GetMapping("/findAllSite")
    @Operation(summary = "查询所有点位")
    public Result<List<RentalKvRes>> findAllSite(String id, @RequestParam("type") Integer type,Integer rentalType){
        return Result.success(rentalService.findAllSite(id,type,rentalType));
    }

    /**
     * 删除租赁
     * @return
     */
    @GetMapping("/del")
    @Operation(summary = "删除租赁")
    @PreAuthorize("@el.check('backend:business:rental:del')")
    public Result delById(@RequestParam("rentalId") String rentalId){
        rentalService.delById(rentalId);
        return Result.success();
    }

    /**
     * 查询站点下分区/设备
     * @param req
     * @return
     */
    @PostMapping("/findAllRoomBySiteId")
    @Operation(summary = "查询站点下分区/设备")
    public Result<List<?>> findAllRoomBySiteId(@RequestBody @Validated RentalFindRdReq req){
        return Result.success(rentalService.findAllRoomBySiteId(req));
    }

    @PostMapping("/postpone")
    @Operation(summary = "延期")
    @PreAuthorize("@el.check('backend:business:rental:postpone')")
    public Result postpone(@RequestBody @Validated RentalPostponeReq req){
        rentalService.postpone(req);
        return Result.success();
    }

    @GetMapping("/endService")
    @Operation(summary = "结束服务")
    @PreAuthorize("@el.check('backend:business:rental:endService')")
    public Result endService(@RequestParam("rentalId") String rentalId){
        rentalService.endService(rentalId);
        return Result.success();
    }
}

